﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace mVu
{
  class Pornhub : Site
  {
    public override Dictionary<string, string> GetCategories()
    {
      var categories = new Dictionary<string, string>
                         {
                           {String.Empty, "c="},
                           {"Amateur", "c=3"},
                           {"Anal", "c=35"},
                           {"Asian", "c=1"},
                           {"Ass", "c=4"},
                           {"Babe", "c=5"},
                           {"BBW", "c=6"},
                           {"Big Dick", "c=7"},
                           {"Big Tits", "c=8"},
                           {"Blond", "c=9"},
                           {"Blowjob", "c=13"},
                           {"Bondage", "c=10"},
                           {"Brunette", "c=11"},
                           {"Bukkake", "c=14"},
                           {"Camel Toe", "c=74"},
                           {"Celebrity", "c=12"},
                           {"Compilation", "c=57"},
                           {"Creampie", "c=15"},
                           {"Cumshots", "c=16"},
                           {"Dancing", "c=34"},
                           {"Double Penetration", "c=72"},
                           {"Ebony", "c=17"},
                           {"Euro", "c=55"},
                           {"Female Friendly", "c=73"},
                           {"Fetish", "c=18"},
                           {"Fisting", "c=19"},
                           {"Funny", "c=32"},
                           {"Handjob", "c=20"},
                           {"Hardcore", "c=21"},
                           {"Hentai", "c=36"},
                           {"Interracial", "c=25"},
                           {"Latina", "c=26"},
                           {"Lesbian", "c=27"},
                           {"Masturbation", "c=22"},
                           {"Mature", "c=28"},
                           {"MILF", "c=29"},
                           {"Orgy", "c=2"},
                           {"Outdoor", "c=24"},
                           {"Party", "c=53"},
                           {"Pornstar", "c=30"},
                           {"POV", "c=41"},
                           {"Reality", "c=31"},
                           {"Red Head", "c=42"},
                           {"Rough Sex", "c=67"},
                           {"Small Tits", "c=59"},
                           {"Squirt", "c=69"},
                           {"Teen", "c=37"},
                           {"Threesome", "c=65"},
                           {"Toys", "c=23"},
                           {"Vintage", "c=43"},
                           {"Webcam", "c=61"}
                         };

      return categories;
    }

    public override Dictionary<string, string> GetSorts()
    {
      var sorts = new Dictionary<string, string>
                    {
                      {String.Empty, String.Empty},
                      {"Being Watched", "o=bw"},
                      {"Most Recent", "o=mr"},
                      {"Most Viewed", "o=mv"},
                      {"Top Rated", "o=tr"},
                      {"Longest", "o=lg"}
                    };
      return sorts;
    }

    public override Dictionary<string, string> GetMediaTypes()
    {
      var types = new Dictionary<string, string>
                    {
                      {"FLV", "FLV"}
                    };
      return types;
    }

    public override List<Thumbnail> GetThumbnails(string baseAddress, string sort, string query, int page)
    {
      var list = new List<Thumbnail>();

      try
      {
        if (String.IsNullOrEmpty(baseAddress))
        {
          baseAddress = "c=";
        }

        var address = String.Format("http://www.pornhub.com/video");
        address = address + (String.IsNullOrEmpty(query) ? String.Empty : String.Format("/search?search={0}", query));
        address = address + (String.IsNullOrEmpty(query) ? String.Format("?{0}", baseAddress) : String.Format("&{0}", baseAddress));
        address = address + (String.IsNullOrEmpty(sort) ? String.Empty : String.Format("&{0}", sort));
        address = address + (String.Format("&page={0}", page));
        var result = GetPageSource(address);

        if (!String.IsNullOrEmpty(result))
        {
          var rh = new SgmlReaderDll.SGMLReaderHelper();
          var xhtml = rh.ProcessString(result);

          var doc = XDocument.Parse(xhtml);
          var ns = XNamespace.Get("http://www.w3.org/1999/xhtml");

          list.AddRange(from node in doc.Descendants(ns + "li")
                        where node.Attribute("class") != null && node.Attribute("class").Value.StartsWith("videoblock")
                        select new Thumbnail
                                 {
                                   Image = node.Descendants(ns + "img").First().Attribute("src").Value,
                                   Url = node.Descendants(ns + "a").First().Attribute("href").Value,
                                   Title = node.Descendants(ns + "a").First().Attribute("title").Value,
                                   Length = String.Format("({0})", node.Descendants(ns + "var").Where(n => n.Attribute("class") != null && n.Attribute("class").Value == "duration").Select(n => n.Value).First())
                                 });
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return list;
    }

    public override Dictionary<string, string> GetVideoPaths(string url)
    {
      var dictionary = new Dictionary<string, string>();

      try
      {
        if (!String.IsNullOrEmpty(url))
        {
          var result = GetPageSource(url);

          if (!String.IsNullOrEmpty(result))
          {
            var start = result.IndexOf("http://www.pornhub.com/embed_player.php?id=");
            var end = result.IndexOf("\"", start + 1);

            result = GetPageSource(result.Substring(start, end - start));

            if (!String.IsNullOrEmpty(result))
            {
              var rh = new SgmlReaderDll.SGMLReaderHelper();
              var xhtml = rh.ProcessString(result);

              var doc = XDocument.Parse(xhtml);

              dictionary.Add("FLV", doc.Descendants("flv_url").First().Value);
            }
          }
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return dictionary;
    }
  }
}
